using System;
using System.Windows.Markup;

using Supremacy.Text;

namespace Supremacy.Markup
{
    [MarkupExtensionReturnType(typeof(string))]
    public sealed class LocalizedStringExtension : MarkupExtension
    {
        public object Group { get; set; }
        public string Entry { get; set; }

        #region Overrides of MarkupExtension
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            var textDatabase = LocalizedTextDatabase.Instance;

            if (this.Group == null)
            {
                if (string.IsNullOrWhiteSpace(this.Entry))
                    return "{! Group and Key Required !}";

                return string.Format("{{! Group Required for Entry {0} !}}", this.Entry);
            }

            LocalizedTextGroup group;

            if (!textDatabase.Groups.TryGetValue(this.Group, out group))
                return string.Format("{{! Unknown Text Group: {0} !}}", group);

            LocalizedString entry;

            if (!group.Entries.TryGetValue(this.Entry, out entry))
                return group.DefaultLocalText;

            return entry.LocalText;
        }
        #endregion
    }
}